<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:15 $)
// $RCSfile: staff_contacts.php,v $ : $Revision: 1.3 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_html.php");
require_once ("./includes/functions_customfields.php");
require_once ("./modules/teamwork/functions_teamwork.php");
require_once ("./modules/teamwork/functions_contacts.php");
require_once ("./modules/teamwork/functions_contactsrender.php");
$template->loadLanguageSection("staff_twcontacts");

if ($eventaction != "editcontact")
{
	$interface->assignQuickLink($_SWIFT["language"]["insertcontact"], "index.php?_m=teamwork&_a=insertcontact", "icon_contacts.gif");
	$interface->assignQuickLink($_SWIFT["language"]["addresscards"], "index.php?_m=teamwork&_a=contacts&viewtype=".CONTACTVIEW_ADDRESSCARDS, "icon_contactview.gif");
	$interface->assignQuickLink($_SWIFT["language"]["detailedcards"], "index.php?_m=teamwork&_a=contacts&viewtype=".CONTACTVIEW_DETAILEDCARDS, "icon_contactview.gif");
	$interface->assignQuickLink($_SWIFT["language"]["detaillist"], "index.php?_m=teamwork&_a=contacts&viewtype=".CONTACTVIEW_DETAILLIST, "icon_contactview.gif");
	$interface->assignQuickLink($_SWIFT["language"]["phonelist"], "index.php?_m=teamwork&_a=contacts&viewtype=".CONTACTVIEW_PHONELIST, "icon_contactview.gif");
	$interface->assignQuickLink($_SWIFT["language"]["bycompany"], "index.php?_m=teamwork&_a=contacts&viewtype=".CONTACTVIEW_BYCOMPANY, "icon_contactview.gif");
}


/**
* ###############################################
* CONTACTS
* ###############################################
*/
if ($eventaction == "contacts")
{
	if ($_GET["do"] == "delete" && !empty($_GET["contactid"]))
	{
//		$infomessage = $_SWIFT["language"]["ardelconfirm"];

//		deleteAlertRules(array($_GET["alertruleid"]));
	}

	if (!empty($_GET["insertcontact"]))
	{
		$infomessage = sprintf($_SWIFT["language"]["contactinsertconfirm"], htmlspecialchars($_GET["insertcontact"]));
	} else if (!empty($_GET["updatecontact"])) {
		$infomessage = sprintf($_SWIFT["language"]["contactupdateconfirm"], htmlspecialchars($_GET["updatecontact"]));
	} else if (!empty($_GET["delcontact"])) {
		$infomessage = sprintf($_SWIFT["language"]["contactdelconfirm"], htmlspecialchars($_GET["delcontact"]));
	}

	if (empty($_REQUEST["viewtype"]))
	{
		$_viewtype = CONTACTVIEW_ADDRESSCARDS;
	} else {
		$_viewtype = $_REQUEST["viewtype"];
	}

	$interface->staffHeader($_SWIFT["language"]["teamwork"]." &gt; ".$_SWIFT["language"]["contacts"], 8);

	$interface->staffNavBar('<a href="index.php?_m=teamwork&_a=calendar" title="'.$_SWIFT["language"]["teamwork"].'">'.$_SWIFT["language"]["teamwork"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=contacts" title="'.$_SWIFT["language"]["conacts"].'">'.$_SWIFT["language"]["contacts"].'</a>', "", 7);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	$options = array();
	$options["tabs"][0]["title"] = $_SWIFT["language"]["addresscards"];
	$options["tabs"][0]["url"] = "index.php?_m=teamwork&_a=contacts&viewtype=".CONTACTVIEW_ADDRESSCARDS;
	$options["tabs"][0]["selected"] = iif($_viewtype==CONTACTVIEW_ADDRESSCARDS, true, false);
	$options["tabs"][0]["id"] = CONTACTVIEW_ADDRESSCARDS;
	$options["tabs"][0]["onclick"] = "this.blur(); switchGridTab('addresscards', 'contacts');fetchData('addresscards', '0');oldKeyword='';";
	$options["tabs"][1]["title"] = $_SWIFT["language"]["detailedcards"];
	$options["tabs"][1]["url"] = "index.php?_m=teamwork&_a=contacts&viewtype=".CONTACTVIEW_DETAILEDCARDS;
	$options["tabs"][1]["selected"] = iif($_viewtype==CONTACTVIEW_DETAILEDCARDS, true, false);
	$options["tabs"][1]["id"] = CONTACTVIEW_DETAILEDCARDS;
	$options["tabs"][1]["onclick"] = "this.blur(); switchGridTab('detailedcards', 'contacts');fetchData('detailedcards', '0');oldKeyword='';";
	$options["tabs"][2]["title"] = $_SWIFT["language"]["detaillist"];
	$options["tabs"][2]["url"] = "index.php?_m=teamwork&_a=contacts&viewtype=".CONTACTVIEW_DETAILLIST;
	$options["tabs"][2]["selected"] = iif($_viewtype==CONTACTVIEW_DETAILLIST, true, false);
	$options["tabs"][2]["id"] = CONTACTVIEW_DETAILLIST;
	$options["tabs"][3]["title"] = $_SWIFT["language"]["phonelist"];
	$options["tabs"][3]["url"] = "index.php?_m=teamwork&_a=contacts&viewtype=".CONTACTVIEW_PHONELIST;
	$options["tabs"][3]["selected"] = iif($_viewtype==CONTACTVIEW_PHONELIST, true, false);
	$options["tabs"][3]["id"] = CONTACTVIEW_PHONELIST;
	$options["tabs"][4]["title"] = $_SWIFT["language"]["bycompany"];
	$options["tabs"][4]["url"] = "index.php?_m=teamwork&_a=contacts&viewtype=".CONTACTVIEW_BYCOMPANY;
	$options["tabs"][4]["selected"] = iif($_viewtype==CONTACTVIEW_BYCOMPANY, true, false);
	$options["tabs"][4]["id"] = CONTACTVIEW_BYCOMPANY;

	?>
	<script language="Javascript">
		function recalcContactHeight()
		{
			column1obj = browserObject("contactcolumn1");
			column2obj = browserObject("contactcolumn2");
			compareobj = browserObject("compareheight");
			if (!column1obj || !column2obj || !compareobj)
			{
				return false;
			}

			if (!compareobj.offsetHeight)
			{
				return;
			}

			if (ie == true)
			{
				var finalHeight = (compareobj.offsetHeight-15);
			} else {
				var finalHeight = (compareobj.offsetHeight-15)+"px";
			}

			column1obj.style.height = finalHeight;
			column2obj.style.height = finalHeight;
		}

		function resetQuery(nowordreset)
		{
			document.realtimesearch.searchquery.value = "";

			if (nowordreset != true)
			{
				oldKeyword = "";
			}
			document.realtimesearch.searchquery.focus();
		}

		<?
		if ($_viewtype == CONTACTVIEW_ADDRESSCARDS || $_viewtype == CONTACTVIEW_DETAILEDCARDS)
		{
			?>
			selectedTab = "<?=$_viewtype?>";
			<?
		}
		?>
	</script>
	<?

	// ======= AJAX BASED CARD VIEW =======
	if ($_viewtype == CONTACTVIEW_ADDRESSCARDS || $_viewtype == CONTACTVIEW_DETAILEDCARDS)
	{
		$loadingdata = '<table border="0" cellpadding="3" cellspacing="1" width="100%" class="tborder"><tr><td class="row1"><span class="smalltext"><img src="'. $_SWIFT["themepath"] .'loadingcircle.gif" border="0" align="absmiddle" />&nbsp;'. $_SWIFT["language"]["loading"] .'</span></td></tr></table><BR /> ';

		echo '<table cellspacing="0" cellpadding="0" border="0" width="100%" id="caltableopt"><tr style="height: 1em"><td align="left"><div id="calopt"><ul id="tab">';
		foreach ($options["tabs"] as $key=>$val)
		{
			echo '<li><a'. iif($val["selected"]==true, ' class="currenttab"') .' href="'. iif(empty($val["onclick"]),$val["url"],"#") .'" onClick="'. $val["onclick"] .'" id="'. $val["id"] .'" title="'. $val["title"] .'">'. $val["title"] .'</a></li>';
		}
		echo '</ul></div></td></tr>'.SWIFT_CRLF;

		echo '<tr style="height: 1em"><td align="left">'.SWIFT_CRLF;

		echo '<div id="tab_'. CONTACTVIEW_ADDRESSCARDS .'" style="DISPLAY: '. iif($_viewtype==CONTACTVIEW_ADDRESSCARDS||$_viewtype==CONTACTVIEW_DETAILEDCARDS, "block", "none") .';" class="tabcontent">';

		echo '<table border="0" cellpadding="3" cellspacing="1" width="100%" class="tborder">';
		echo '<tr><td class="row1"><span class="smalltext"><form name="realtimesearch" id="realtimesearch" action="index.php" method="POST" onSubmit="javascript:return false;">';
		echo $_SWIFT["language"]["realtimesearch"].'&nbsp;&nbsp;<input type="text" name="searchquery" size="40" class="swifttext">&nbsp;&nbsp;[<a href="javascript:resetQuery(true);" id="blue">'. $_SWIFT["language"]["reset"] .'</a>]';
		echo '</form></span></td></tr>';
		echo '<tr><td class="row1"><span class="smalltext">';
		echo '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td align="left" valign="top"><div id="contactscontainer" style="POSITION:relative; WIDTH:100%; HEIGHT:100%;">';

		echo renderContactCards($_viewtype);

		echo '&nbsp;</div></td><td width="4"><img src="'. $_SWIFT["themepath"] .'space.gif" width="4" height="1" /></td><td width="1" class="contactsplit"><div id="compare"><img src="'. $_SWIFT["themepath"] .'space.gif" width="1" height="1" /></td><td width="4"><img src="'. $_SWIFT["themepath"] .'space.gif" width="4" height="1" /></td><td align="right" valign="top" width="20" id="compareheight">';

		$searchfilter = array("a", "b", "c", "d", "e", "f", "g", "h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
		foreach ($searchfilter as $key=>$val)
		{
			echo '<div class="boxcontainer" onMouseOver="javascript:this.className=\'boxcontainerhover\';" onMouseOut="javascript:this.className=\'boxcontainer\';" onClick="javascript:fetchData(selectedTab, \''. $val .'\');resetQuery(false);">'. $val .'</div>';
		}

		echo '</td></tr></table>';
		echo '</span></td></tr></table><BR />';

		echo '</div>';

		echo '</td></tr></table>';

	?>
	<script language="Javascript">
		setTimeout("keywordCheckContacts();", 1000);
		document.realtimesearch.searchquery.focus();
		recalcContactHeight();
	</script>
	<?



	// ======= GRID VIEW =======
	} else {
		$options["recordsperpage"] = "10";

		$_gridsort = getContactGridSort($_viewtype);
		$options["sortby"] = $_gridsort[0];
		$options["sortorder"] = $_gridsort[1];

		$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
		$options["massaction"][0]["callback"] = "_maDeleteContacts";

		$options["idname"] = "contactid";
		$options["quicksearch"] = true;

		$options["appendurl"] = "&viewtype=".$_viewtype;
		$options["hiddenfields"]["viewtype"] = $_viewtype;

		$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["title"];
		$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'alertrules` WHERE `title` LIKE \'%$_searchstr%\' AND ((`alertscope` = \''. ALERT_PUBLIC .'\') OR (`alertscope` = \''. ALERT_PRIVATE .'\' AND `staffid` = \''. intval($_SWIFT["staff"]["staffid"]) .'\')) $_sortjoin;';

		$options["callback"] = "_contactFields";

		$fields = getContactGridFields($_viewtype);

		$grid = new Grid();

		$grid->start("cl".$_viewtype, $_SWIFT["language"]["contacts"], 'SELECT * FROM `'. TABLE_PREFIX .'contacts` WHERE (`contacttype` = \''. SWIFTPUBLIC .'\') OR (`contacttype` = \''. SWIFTPRIVATE .'\' AND `staffid` = \''. intval($_SWIFT["staff"]["staffid"]) .'\') $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'contacts` WHERE (`contacttype` = \''. SWIFTPUBLIC .'\') OR (`contacttype` = \''. SWIFTPRIVATE .'\' AND `staffid` = \''. intval($_SWIFT["staff"]["staffid"]) .'\');', 'SELECT * FROM `'. TABLE_PREFIX .'contacts` WHERE ((`contacttype` = \''. SWIFTPUBLIC .'\') OR (`contacttype` = \''. SWIFTPRIVATE .'\' AND `staffid` = \''. intval($_SWIFT["staff"]["staffid"]) .'\')) AND `fullname` LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);

		printInfoBox($_SWIFT["infomessage"]);
		printErrorBox($_SWIFT["errormessage"]);

		$grid->display("cl".$_viewtype);
	}

	$interface->staffFooter();










/**
* ###############################################
* INSERT CONTACT
* ###############################################
*/
} else if ($eventaction == "insertcontact") {
	if ($_POST["step"] == 1)
	{
		if (trim($_POST["fullname"]) == "")
		{
			$errormessage = $_SWIFT["language"]["fullnameempty"];
		} else if (!checkCustomFieldContent(CUSTOMFIELD_CONTACT)) {
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			$contactid = insertContact($_POST["contacttype"], contactPostToArguments());
			if ($contactid)
			{
				updateCustomFields(CUSTOMFIELD_CONTACT, $contactid);
			}

			printRedirect(sprintf($_SWIFT["language"]["contactinsertconfirm"], htmlspecialchars($_POST["fullname"])), "index.php?_m=teamwork&_a=contacts&insertcontact=".urlencode($_POST["fullname"]));
			exit;
		}
	}

	$template->assign("calendar", true);

	$interface->staffHeader($_SWIFT["language"]["teamwork"]." &gt; ".$_SWIFT["language"]["insertcontact"], 8);

	$interface->staffNavBar('<a href="index.php?_m=teamwork&_a=calendar" title="'.$_SWIFT["language"]["teamwork"].'">'.$_SWIFT["language"]["teamwork"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=contacts" title="'.$_SWIFT["language"]["conacts"].'">'.$_SWIFT["language"]["contacts"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=insertcontact" title="'.$_SWIFT["language"]["insertcontact"].'">'.$_SWIFT["language"]["insertcontact"].'</a>', "", 7);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderContactForm(INSERT);

	$template->assign("backurl", "index.php?_m=teamwork&_a=contacts");

	$interface->staffFooter();







/**
* ###############################################
* EDIT CONTACT
* ###############################################
*/
} else if ($eventaction == "editcontact") {
	$_contact = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."contacts` AS contacts LEFT JOIN `". TABLE_PREFIX ."calendardata` AS calendardata ON (calendardata.datatype = '". TEAMWORK_CONTACT ."' AND calendardata.typeid = contacts.contactid) WHERE contacts.contactid = '". intval($_REQUEST["contactid"]) ."';");
	if (empty($_contact["contactid"]) || ($_contact["contacttype"] == SWIFTPRIVATE && $_contact["staffid"] != $_SWIFT["staff"]["staffid"]))
	{
		trigger_error($_SWIFT["language"]["invalidcontact"], E_USER_ERROR);
	}

	if ($_GET["mode"] == "delete")
	{
		deleteContacts(array($_contact["contactid"]));

		printRedirect(sprintf($_SWIFT["language"]["contactdelconfirm"], htmlspecialchars($_contact["fullname"])), "index.php?_m=teamwork&_a=contacts&delcontact=".urlencode($_contact["fullname"]));
		exit;
	}

	if ($_POST["step"] == 1)
	{
		if (trim($_POST["fullname"]) == "")
		{
			$errormessage = $_SWIFT["language"]["fullnameempty"];
		} else if (!checkCustomFieldContent(CUSTOMFIELD_CONTACT)) {
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			updateContact($_contact["contactid"], $_POST["contacttype"], contactPostToArguments());
			updateCustomFields(CUSTOMFIELD_CONTACT, $_contact["contactid"]);

			printRedirect(sprintf($_SWIFT["language"]["contactupdateconfirm"], htmlspecialchars($_POST["fullname"])), "index.php?_m=teamwork&_a=contacts&updatecontact=".urlencode($_POST["fullname"]));
			exit;
		}
	}

	$_POST = array_merge($_POST, $_contact);

	$template->assign("calendar", true);

	if ($_REQUEST["mode"] == "edit")
	{
		$modetext = $_SWIFT["language"]["editcontact"];
	} else {
		$modetext = $_SWIFT["language"]["viewcontact"];
	}

	$template->assign("itemoptiontitle", $_SWIFT["language"]["contactoptions"]);
	if ($_REQUEST["mode"] == "edit")
	{
		$interface->assignItemOption($_SWIFT["language"]["viewcontact"], "index.php?_m=teamwork&_a=editcontact&contactid=".intval($_contact["contactid"])."&mode=view", "icon_contacts.gif");
	} else {
		$interface->assignItemOption($_SWIFT["language"]["editcontact"], "index.php?_m=teamwork&_a=editcontact&contactid=".intval($_contact["contactid"])."&mode=edit", "icon_contacts.gif");
	}
	$interface->assignItemOption($_SWIFT["language"]["deletecontact"], "", "icon_delete.gif", "", 'doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=teamwork&_a=editcontact&contactid='.intval($_contact["contactid"]).'&mode=delete\');');

	$interface->staffHeader($_SWIFT["language"]["teamwork"]." &gt; ".$_SWIFT["language"]["editcontact"], 8);

	$interface->staffNavBar('<a href="index.php?_m=teamwork&_a=calendar" title="'.$_SWIFT["language"]["teamwork"].'">'.$_SWIFT["language"]["teamwork"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=contacts" title="'.$_SWIFT["language"]["conacts"].'">'.$_SWIFT["language"]["contacts"].'</a> &raquo; <a href="index.php?_m=teamwork&_a=editcontact&contactid='. intval($_contact["contactid"]) .'" title="'.$modetext.'">'.$modetext.' ('. htmlspecialchars($_contact["fullname"]) .')</a>', "", 7);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderContactForm(iif($_REQUEST["mode"]=="edit", EDIT, DISPLAY));

	$template->assign("backurl", "index.php?_m=teamwork&_a=contacts");

	$interface->staffFooter();


}


?>